package com.compass.mobile.ui.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import androidx.leanback.widget.OnChildViewHolderSelectedListener
import androidx.recyclerview.widget.RecyclerView
import com.compass.mobile.R
import com.compass.mobile.databinding.ActivityMyOrderBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.Dictionaries
import com.compass.mobile.presenters.DictionariesPresenter
import com.compass.mobile.ui.fragment.MyInspectReportFragment
import com.compass.mobile.ui.fragment.MyTestReportFragment
import com.compass.mobile.ui.fragment.NuclearMedicineReportFragment
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 我的报告单
 * Date:2024/6/20 16:47
 */
class MyReportActivity : BaseVBActivity<ActivityMyOrderBinding>() {

    companion object {
        fun start(context: Context, reportType: String?) {
            val intent = Intent(context, MyReportActivity::class.java)
            intent.putExtra("reportType", reportType)
            context.startActivity(intent)
        }
    }

    private var list: MutableList<Dictionaries> = mutableListOf()
    private var reportType: String? = null
    private lateinit var myReportFragment: MyTestReportFragment
    private lateinit var myInspectReportFragment: MyInspectReportFragment//我的检验报告单
    private lateinit var nuclearMedicineReportFragment: NuclearMedicineReportFragment//核医学报告
    override fun initView(savedInstanceState: Bundle?) {
        reportType = intent.getStringExtra("reportType")
        myInspectReportFragment = MyInspectReportFragment()
        myReportFragment = MyTestReportFragment()
        nuclearMedicineReportFragment = NuclearMedicineReportFragment()


    }


    override fun initData() {
        when (reportType) {
            "LIS" -> {
                //检验
                mBinding.gridCase.gone()
                displaySelectedFragment(myInspectReportFragment)
            }

            "PACS" -> {
                //检查
                mBinding.gridCase.visible()
                displaySelectedFragment(myReportFragment)
                list.add(Dictionaries("门诊", "0"))
                list.add(Dictionaries("住院", "1"))
                initCaseTitle(list)

            }

            "NUCLEAR" -> {
                //核医学
                mBinding.gridCase.gone()
                displaySelectedFragment(nuclearMedicineReportFragment)
            }

            else -> {
                mBinding.gridCase.gone()
                showToast("暂未开放")
            }


        }


    }


    // 根据需要显示或替换Fragment
    private fun displaySelectedFragment(fragment: Fragment) {
        val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragmentChild, fragment)
        fragmentTransaction.commit()


    }

    @SuppressLint("RestrictedApi")
    fun initCaseTitle(dictionaries: MutableList<Dictionaries>?) {
        mBinding.gridCase.verticalSpacing = dpToPx(10)

        val arrayObjectAdapter = ArrayObjectAdapter(DictionariesPresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            this,
            arrayObjectAdapter, null
        )
        mBinding.gridCase.setAdapter(itemBridgeAdapter)
        arrayObjectAdapter.addAll(0, dictionaries)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_SMALL, false
        )
        mBinding.gridCase.setOnChildViewHolderSelectedListener(onChildViewHolderSelectedListener)
        mBinding.gridCase.requestFocus()

    }

    private val onChildViewHolderSelectedListener: OnChildViewHolderSelectedListener = object :
        OnChildViewHolderSelectedListener() {
        override fun onChildViewHolderSelected(
            parent: RecyclerView?,
            child: RecyclerView.ViewHolder?,
            position: Int,
            subposition: Int
        ) {

            val item = list[position]
            myReportFragment.setSelect(item.value, reportType)


        }
    }
}